//
//  BMCustomButton.m
//  bigmama
//
//  Created by hychen on 16/9/3.
//  Copyright © 2016年 . All rights reserved.
//

#import "BMCustomButton.h"

@implementation BMCustomButton

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGSize textSize = [self.titleLabel.text sizeWithFont:self.titleLabel.font maxH:MAXFLOAT];
    
    CGSize imageSize = CGSizeZero;
    if (self.imageWidth && self.imageHeight) {
        imageSize = CGSizeMake(self.imageWidth, self.imageHeight);
    }
    else {
        imageSize = CGSizeMake(self.imageView.image.size.width, self.imageView.image.size.height);
    }
    
    if (self.customStyle == CustomButtonStyleImageBottomTitleTop) {
        CGFloat imageTextHeight = textSize.height + imageSize.height;
        CGFloat spacing = self.frame.size.height - imageTextHeight > 0 ? (self.frame.size.height - imageTextHeight) / 3 : 0;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.frame = CGRectMake(0, spacing, self.frame.size.width, textSize.height);
        self.imageView.frame = CGRectMake((self.frame.size.width - imageSize.width)*0.5, CGRectGetMaxY(self.imageView.frame)+spacing, imageSize.width, imageSize.height);
    }
    else if (self.customStyle == CustomButtonStyleImageTopTitleBottom) {
        CGFloat imageTextHeight = textSize.height + imageSize.height;
        CGFloat spacing = self.frame.size.height - imageTextHeight > 0 ? (self.frame.size.height - imageTextHeight) / 3 : 0;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.imageView.frame = CGRectMake((self.frame.size.width - imageSize.width)*0.5, spacing, imageSize.width, imageSize.height);
        self.titleLabel.frame = CGRectMake(0, CGRectGetMaxY(self.imageView.frame) + spacing, self.frame.size.width, textSize.height);
    }
    else if (self.customStyle == CustomButtonStyleImageRightTitleLeft) {
        CGFloat imageTextWidth = textSize.width + imageSize.width;
        if (imageTextWidth > self.frame.size.width-5) {
            textSize.width = self.frame.size.width - imageSize.width;
            self.titleLabel.frame = CGRectMake(0, (self.frame.size.height-textSize.height)*0.5, textSize.width, textSize.height);
        }
        else {
            CGFloat startX = (self.frame.size.width - imageTextWidth) * 0.5;
            self.titleLabel.frame = CGRectMake(startX, (self.frame.size.height-textSize.height)*0.5, textSize.width, textSize.height);
        }
        self.imageView.frame = CGRectMake(CGRectGetMaxX(self.titleLabel.frame)+5, (self.frame.size.height-imageSize.height)*0.5, imageSize.width, imageSize.height);
    }
    else if (self.customStyle == CustomButtonStyleImageLeftTitleRight){
        CGFloat imageTextWidth = textSize.width + imageSize.width;
        if (imageTextWidth > self.frame.size.width-5) {
            textSize.width = self.frame.size.width - imageSize.width;
            self.imageView.frame = CGRectMake(0, (self.frame.size.height-imageSize.height)*0.5, imageSize.width, imageSize.height);
        }
        else {
            CGFloat startX = (self.frame.size.width - imageTextWidth) * 0.5;
            self.imageView.frame = CGRectMake(startX, (self.frame.size.height-imageSize.height)*0.5, imageSize.width, imageSize.height);
        }
        self.titleLabel.frame = CGRectMake(CGRectGetMaxX(self.imageView.frame)+5, (self.frame.size.height-textSize.height)*0.5, textSize.width, textSize.height);
    }
    else if (self.customStyle == CustomButtonStyleImageTopTitleBottomCenter) {
        CGFloat imageTextHeight = textSize.height + imageSize.height;
        CGFloat spacing = (self.frame.size.height - imageTextHeight)*0.4;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.imageView.frame = CGRectMake((self.frame.size.width - imageSize.width)*0.5, spacing, imageSize.width, imageSize.height);
        self.titleLabel.frame = CGRectMake(0, CGRectGetMaxY(self.imageView.frame) + 20, self.frame.size.width, textSize.height);
    }
}

- (void)setCustomStyle:(BMCustomButtonStyle)customStyle {
    _customStyle = customStyle;
    [self layoutIfNeeded];
}

@end
